package com.artPreter.controller.pc;

import com.artPreter.pojo.*;
import com.artPreter.pojo.ArtistTemp;
import com.artPreter.pojo.dto.ArtistRelativeDTO;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.artPreter.utils.ExchageArtTypeUtils.*;
import static com.artPreter.utils.PageUtils.getPageList;

@Controller
public class ArtistController {

    @Autowired
    private IArtistContentSectionService artistContentSectionService;
    @Autowired
    private IArtistTempService artistTempService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtworkService artwrokService;
    @Autowired
    private IArtistSectionTempService artistSectionTempService;
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private IArtistRelativeService artistRelativeService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IArtworkService artWorkService;
    @Autowired
    private IEventExhibitionService eventExhibitionService;
    @Autowired
    private IArtworkOtherPicService artworkOtherPicService;

    @GetMapping("/artist")
    public String artist(Integer id, Model model) {
        // 头部菜单
        List<DepartContentSection> menuList = departContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        String artistId = "";
        Artist artist = artistService.getArtistById(id);
        if(artist==null||artist.getArtistId()==null){
            return "redirect:/";
        }else{
            artistId = artist.getArtistId();

        }


        ArtistTemp artistTemp = new ArtistTemp();
        artistTemp.setArtistId(artistId);

        ArtistTemp artistTempItem = artistTempService.getArtistTemp(artistTemp);
        if (artistTempItem == null) {
            return "redirect:/";
        }
        ArtistSectionTemp artistSectionTemp = new ArtistSectionTemp();
        artistSectionTemp.setArtistId(artistId);
        List<ArtistSectionTemp> artistSectionTemps = artistSectionTempService.getList(artistSectionTemp);
        if(artistSectionTemps.size() >0 ){
            for (int i = 0; i < artistSectionTemps.size(); i++) {
                if(artistSectionTemps.get(i).getIsDefault().equals(1)){
                    return "redirect:/artist/page?id="+id+"&sid="+artistSectionTemps.get(i).getId();
                }
            }
        }else{
            return "redirect:/";
        }

        return "redirect:/";
    }

    /**
     *
     * @param wid
     * @param sid
     * @param id 艺术家id
     * @param model
     * @return
     */
    @GetMapping("/artist/artwork/detail")
    public String artistArtworkDetail(Integer wid,Integer sid,Integer id, Model model) {
        model.addAttribute("wid",wid);
        model.addAttribute("sid",sid);
        model.addAttribute("id",id);
        // 菜单
        List<ArtistSectionTemp> artistSectionTemps = artistSectionTempService.getListBySid(sid);
        model.addAttribute("artistSectionTemps",artistSectionTemps);
        for (int i = 0; i < artistSectionTemps.size(); i++) {
            if(artistSectionTemps.get(i).getId().equals(sid)){
                model.addAttribute("backName",artistSectionTemps.get(i).getName());
            }
        }

        Artwork artwork = artWorkService.getArtWorkById(wid);
        if(artwork != null){
            List<ArtType> artTypes = artTypeService.getList();
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())){
                    artwork.setArtTypeName(artTypes.get(i).getName());
                }
            }
            model.addAttribute("artwork",artwork);
        }
        List<ArtworkOtherPic> artworkOtherPics = artworkOtherPicService.getArtworkAllPicList(artwork);
        model.addAttribute("artworkOtherPics",artworkOtherPics);

        ArtistTemp artistTempItem = new ArtistTemp();
        artistTempItem.setArtistId(artwork.getArtistId());
        ArtistTemp artistTemp = artistTempService.getArtistTemp(artistTempItem);
        if(artistTemp!= null){
            model.addAttribute("artistTemp",artistTemp);
        }else{
            return "redirect:/";
        }



        return "pc/artist/simple_artwork_detail";
    }

    /**
     *
     * @param aid 文章id
     * @param sid 板块id
     * @param id 艺术家id
     * @param model
     * @return
     */
    @GetMapping("/article/article/detail")
    public String departArticleDetail(Integer aid,Integer sid,Integer id, Model model) {
        System.out.println("艺术家文章详情");
        model.addAttribute("aid",aid);
        model.addAttribute("sid",sid);
        model.addAttribute("id",id);
        // 菜单
        List<ArtistSectionTemp> artistSectionTemps = artistSectionTempService.getListBySid(sid);
        model.addAttribute("artistSectionTemps",artistSectionTemps);
        for (int i = 0; i < artistSectionTemps.size(); i++) {
            if(artistSectionTemps.get(i).getId().equals(sid)){
                model.addAttribute("backName",artistSectionTemps.get(i).getName());
            }
        }

        Article article = articleService.getArticleByNumberId(aid);

        if(article.getLink()!=null&&!article.getLink().equals("")){
            // 文章链接
            String link = article.getLink();
            return "redirect:"+link;
        }


        if(article != null){
            model.addAttribute("article",article);
        }
        System.out.println("文章内容"+article);

        ArtistTemp artistTempItem = new ArtistTemp();
        artistTempItem.setArtistId(article.getPlateId());
        ArtistTemp artistTemp = artistTempService.getArtistTemp(artistTempItem);
        if(artistTemp!= null){
            model.addAttribute("artistTemp",artistTemp);
        }else{
            return "redirect:/";
        }



        return "pc/artist/simple_article_detail";


    }

    @GetMapping("/artist/page")
    public String artistPage(Integer id,Integer sid,Integer type,Integer currentPage,String name, Model model) {

        model.addAttribute("id",id);
        model.addAttribute("sid",sid);
        model.addAttribute("type",type);
        model.addAttribute("name",name);

        String artistId = "";
        Artist artist = artistService.getArtistById(id);
        if(artist==null||artist.getArtistId()==null){
            return "redirect:/";
        }else{
            artistId = artist.getArtistId();

        }
        if(currentPage==null){
            currentPage = 1;
        }
        model.addAttribute("currentPage",currentPage);

        ArtistSectionTemp asTemp =  artistSectionTempService.getById(sid);
        String sectionId = "";
        if(asTemp==null||asTemp.getSectionId()==null){
            return "redirect:/";
        }else{
            sectionId = asTemp.getSectionId();

        }

        ArtistSectionTemp artistSectionTemp = new ArtistSectionTemp();
        artistSectionTemp.setArtistId(artistId);
        model.addAttribute("artistId",artistId);
        model.addAttribute("sectionId",sectionId);
        List<ArtistSectionTemp> artistSectionTemps = artistSectionTempService.getList(artistSectionTemp);
        List<ArtType> artTypeList = artTypeService.getList();
        if(artist == null){
            return "redirect:/";
        }else{
            if(artist.getEmail()==null||artist.getEmail().equals("")){
                artist.setEmail(null);
            }
            if(artist.getErweima2()==null||artist.getErweima2().equals("")){
                artist.setErweima2(null);
            }
            if(artist.getPhone()==null||artist.getPhone().equals("")){
                artist.setPhone(null);
            }
            model.addAttribute("artist",artist);
            // 获取艺术家对艺术家关系
            List<ArtistRelativeDTO> departs = artistRelativeService.getListByArtistId(artist);
            // 艺术家机构
            model.addAttribute("departs",departs);
            // 艺术家称号
            model.addAttribute("honors",artist.getHonorList());
            // 艺术家擅长艺术
            List<String> artTypes = exchangeArtType(artist.getArtistType(), artTypeList);
            model.addAttribute("artTypes",artTypes);

            // 艺术家二维码
        }

        String tempPath = "";

        for (int i = 0; i < artistSectionTemps.size(); i++) {
            if(artistSectionTemps.get(i).getSectionId().equals(sectionId)){
                tempPath = artistSectionTemps.get(i).getTempAddr();
                ArtistContentSection artistContentSection = new ArtistContentSection();
                artistContentSection.setSectionId(sectionId);
                artistContentSection.setArtistId(artistId);
                System.out.println("模板信息"+artistSectionTemps.get(i));
                if(artistSectionTemps.get(i).getName().equals("作品在线")){
                    tempPath = "pc/artist/simple_artwork_online_list";
                }
                List<ArtistContentSection> artistContentSectionServiceList = artistContentSectionService.getList(artistContentSection);
                // 文章 5
                List<Article> articles = new ArrayList<>();
                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片 4
                List<Picture> pictures = new ArrayList<>();

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                for (int j = 0; j < artistContentSectionServiceList.size(); j++) {
                    // 单页 1
                    if(artistContentSectionServiceList.get(j).getContentType().equals(1)){
                        pages.add(artistContentSectionServiceList.get(j).getContent());
                    }

                    // 艺术品 3
                    if(artistContentSectionServiceList.get(j).getContentType().equals(3)){


                        if(tempPath.equals("pc/artist/simple_artwork_online_list")){


                            Integer size = 21;
                            Artwork artwork = new Artwork();
                            // 模块id搜索
                            artwork.setContentId(artistContentSectionServiceList.get(j).getContentId());
                            if(type!=null){
                                String typeId = exchangeArtTypeId(type, artTypeList);
                                if(typeId!=null&&!"".equals(typeId)){
                                    artwork.setArtwoktType(typeId);
                                }
                            }

                            // 艺术家作品类型
                            List<ArtType> artworkTypeList = artwrokService.getArtistRelatedArtworkArtTypegetPageList(currentPage, size, artwork);
                            model.addAttribute("artworkArtTypes",exchangeArtTypeList(artworkTypeList,artTypeList));

                            IPage<Artwork> artworkIPage = artwrokService.getRelatedArtworkPageList(artwork,currentPage,size);

                            // 当前页
                            model.addAttribute("currentPage", artworkIPage.getCurrent());
                            // 总页数
                            model.addAttribute("totalPages", artworkIPage.getPages());
                            // 分页页码
                            model.addAttribute("pages", getPageList((int) artworkIPage.getPages(), currentPage, 5));
                            // 分页数据
                            model.addAttribute("artworks", artworkIPage.getRecords());

                            model.addAttribute("artworks",artworkIPage.getRecords());
                        }else{
                            Integer artWorkSize = 21;
                            Artwork artwork = new Artwork();
                            // 模块id搜索
                            artwork.setContentId(artistContentSectionServiceList.get(j).getContentId());
                            if(type!=null){
                                String typeId = exchangeArtTypeId(type, artTypeList);
                                if(typeId!=null&&!"".equals(typeId)){
                                    artwork.setArtwoktType(typeId);
                                }
                            }

                            // 艺术家作品类型
                            List<ArtType> artworkTypeList = artwrokService.getArtistRelatedArtworkArtTypegetPageList(currentPage, artWorkSize, artwork);
                            model.addAttribute("artworkArtTypes",exchangeArtTypeList(artworkTypeList,artTypeList));

                            IPage<Artwork> artworkIPage = artwrokService.getRelatedArtworkPageList(artwork,currentPage,artWorkSize);

                            // 当前页
                            model.addAttribute("currentPage", artworkIPage.getCurrent());
                            // 总页数
                            model.addAttribute("totalPages", artworkIPage.getPages());
                            // 分页页码
                            model.addAttribute("pages", getPageList((int) artworkIPage.getPages(), currentPage, 5));
                            // 分页数据
                            model.addAttribute("artworks", artworkIPage.getRecords());

                            model.addAttribute("artworks",artworkIPage.getRecords());
                        }




                    }
                    // 图片 4
                    if(artistContentSectionServiceList.get(j).getContentType().equals(4)){
                        pictures = pictureService.getArtistRelatedPictureList(artistContentSectionServiceList.get(j),-99);
                        model.addAttribute("pictures",pictures);
                    }
                    // 文章 5
                    if(artistContentSectionServiceList.get(j).getContentType().equals(5)){
                        articles = articleService.getArtistRelateArticleList(artistContentSectionServiceList.get(j),-99);
                        model.addAttribute("articles",articles);
                    }
                    // 超链接 6
                    if(artistContentSectionServiceList.get(j).getContentType().equals(6)){
                        model.addAttribute("link",artistContentSectionServiceList.get(j).getContent());
                        if(artistContentSectionServiceList.get(j).getContent()!= null){
                            return "redirect:"+artistContentSectionServiceList.get(j).getContent();
                        }
                    }
                    // 活动展览
                    if(artistContentSectionServiceList.get(j).getContentType().equals(7)){


                        // 活动展览
                        EventExhibition eventExhibition = new EventExhibition();
                        eventExhibition.setArtistIds(artist.getId().toString());


                        IPage<EventExhibition> eventExhibitionIPage = eventExhibitionService.getPage(eventExhibition,currentPage);

                        // 数据
                        List<EventExhibition> records = eventExhibitionIPage.getRecords();
                        for (int l = 0; l < records.size(); l++) {
                            if(records.get(l).getEndTime().isBefore(LocalDateTime.now())){
                                records.get(l).setIsOver(1);
                            }
                            List<Artist> list = new ArrayList<>();
                            if(records.get(l).getArtistIds()!=null){
                                String[] strings1 = records.get(l).getArtistIds().split("，");
                                String[] strings2 = records.get(l).getArtistNames().split("，");
                                for (int z = 0; z < strings1.length; z++) {
                                    if(!strings1[z].equals("")){
                                        Artist artistItem = new Artist();
                                        artistItem.setId(Integer.valueOf(strings1[z]));
                                        artistItem.setName(strings2[z]);
                                        list.add(artistItem);
                                    }
                                }
                            }
                            records.get(l).setList(list);
                        }


                        model.addAttribute("eventExhibitions",eventExhibitionIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage",eventExhibitionIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages",eventExhibitionIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages",getPageList((int) eventExhibitionIPage.getPages(),currentPage,5));
                    }
                    System.out.println("内容"+artistContentSectionServiceList.get(j));

                }
                ArtistTemp artistTempItem = new ArtistTemp();
                artistTempItem.setArtistId(artistId);
                ArtistTemp artistTemp = artistTempService.getArtistTemp(artistTempItem);
                if(artistTemp!= null){
                    model.addAttribute("artistTemp",artistTemp);
                }else{
                    return "redirect:/";
                }



                // 模板单页数据
                model.addAttribute("pageDetails",pages);


            }
        }
        model.addAttribute("artistSectionTemps",artistSectionTemps);

        // artistTemp/getArtistTemp
        System.out.println("模板路径"+tempPath);


        if(tempPath.equals("")){
            return "redirect:/";
        }else{
            return tempPath;
        }
    }
}
